package com.code.demo.mvp.view;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.code.demo.R;
import com.code.demo.mvp.base.BaseMvpActivity;
import com.code.demo.mvp.contract.FakeContract;
import com.code.demo.mvp.presenter.FakePresenter;

import butterknife.BindView;
import butterknife.OnClick;

import static java.lang.Thread.sleep;

/**
 * Created by yangliguo on 2019/3/1.
 */

public class FakeActivity extends BaseMvpActivity<FakePresenter> implements FakeContract.FakeView {

    @BindView(R.id.fake)
    TextView fake;

    Fragment fragment;

    @Override
    public FakePresenter createPresenter() {
        return new FakePresenter();
    }

    @Override
    public int getLayoutResId(Bundle savedInstanceState) {
        return R.layout.activity_fake;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
    }

    @Override
    public void showUsers(String users) {
    }

    @Override
    public void showTodo(String todo) {
        fake.setText(todo);
    }

    @OnClick(R.id.activity_btn)
    public void onActivityButton() {

        Log.e("fake", "onActivityButton");
        Log.e("fake", android.os.Process.myTid() + "");

        fake.setText("todo");

        if (fragment != null) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.hide(fragment);
            ft.commit();
        }


        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e("fake", "Runnable");
                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.e("fake", android.os.Process.myTid() + "");
                Log.e("fake", "Runnable - getTodo");
                presenter.getTodo();
            }
        }).start();

//        fake.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Log.e("fake", "Runnable - getTodo");
//                Log.e("fake", android.os.Process.myTid()+"");
//                presenter.getTodo();
//            }
//        }, 2000);
    }

    @OnClick(R.id.fragment_btn)
    public void onFragmentButton() {

        Log.e("fake", "onFragmentButton");
        Log.e("fake", android.os.Process.myTid() + "");

        fake.setText("Activity");

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        if (fragment == null) {
            fragment = new FakeFragment();
            ft.add(R.id.fragment_content, fragment);
        }
        ft.show(fragment);
        ft.commit();
    }
}
